<?php
require_once(SITE_ROOT . 'class/db/DBAlliance.class.php');
require_once(SITE_ROOT . 'class/db/DBAllianceMember.class.php');
require_once(SITE_ROOT . 'class/db/DBTeam.class.php');
require_once(SITE_ROOT . 'class/bean/Alliance.class.php');
require_once(SITE_ROOT . 'class/bean/Team.class.php');
require_once(SITE_ROOT . 'class/logic/LogicTeam.class.php');
require_once(SITE_ROOT . 'lib/PHT.php');
require_once(SITE_ROOT . 'config/constants.conf.php');

class LogicFedImport {

	private $oldmembers;
	private $newmembers;
	private $members;
	
	public function __construct() {
		$this->oldmembers = array();
		$this->newmembers = new ArrayObject();
	}

	public function fetchAlliance($allianceID) {
		$HT = $_SESSION['HT'];
		
		$this->oldmembers = array();
		$this->newmembers = new ArrayObject();
	
		if ($HT instanceof CHPPConnection) {
			$htAlliance = $HT->getAllianceDetails($allianceID);
			if ($htAlliance != null || $htAlliance->getId() != 0) {				
				$htAllianceMembers = $HT->getAllianceMembers($allianceID);
								
				$alliance = new Alliance($htAlliance->getId(), trim($htAlliance->getName()), time());
				$alliance->setLogoURL($htAlliance->getLogoUrl());
				$dballiance = new DBAlliance();
				$dballiance->saveAlliance($alliance);
				
				$dballiancemember = new DBAllianceMember();
				$this->oldmembers = $dballiancemember->getAllianceMembershipByAlliance($alliance);
								
				if ($htAllianceMembers->getNumberMembers() >= 100) {
					$HT->clearAlliancesMembers();
					$letters = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0");
					foreach ($letters as $letter) {
						$htAllianceMembers = $HT->getAllianceMembersByLetter($allianceID, $letter);
						$alliance->setNumberMembers($alliance->getNumberMembers() + $htAllianceMembers->getNumberMembers());
						$this->fetchAllianceMembers($alliance, $htAllianceMembers, $HT);
					}
				}
				else {
					$alliance->setNumberMembers($alliance->getNumberMembers() + $htAllianceMembers->getNumberMembers());
					$this->fetchAllianceMembers($alliance, $htAllianceMembers, $HT);
				}
				$dballiancemember->deleteAllianceMembershipByTeams($this->oldmembers);
				$dballiancemember->saveAllianceMemberships($this->newmembers);
			}
			return $alliance;
		}
		else throw new Exception("(0)");
	}
	
	private function fetchAllianceMembers(&$alliance, &$htAllianceMembers, &$HT) {
		$dbteam = new DBTeam();
		
		for ($i=1; $i<=$htAllianceMembers->getNumberMembers(); $i++) {
			$team = $dbteam->getTeamByUserID($htAllianceMembers->getMember($i)->getId());				
			if (!isset($team)) {
				try {
					$htTeam = $HT->getTeamByUserId($htAllianceMembers->getMember($i)->getId());
					
					$team = new Team($htTeam->getTeamId(), $htTeam->getTeamName());
					$team->setUserID($htTeam->getUserId());
					$team->setUserName($htTeam->getLoginName());
					$team->setActivationDate($htTeam->getActivationDate());
					
					$league = new League();
					if ($htTeam->isLeagueLevelAvailable()) {
						$league->setLeagueID($htTeam->getLeagueLevelId());
					}
					else throw new LeagueNotAvailableException($team);
					$team->setLeague($league);
					
					$logicTeam = new LogicTeam();
					$team->setNTCoach($logicTeam->isNTCoach($htTeam));
					$team->setU20Coach($logicTeam->isU20Coach($htTeam));
					
					$dbteam->saveTeam($team);
					
					$team->addAlliance($alliance);
					$this->newmembers->append($team);
				}
				catch (LeagueNotAvailableException $lnae) {}
				catch (Exception $e) {
					if (sha1(strip_tags($e->getMessage())) != "cb29a21059b33c63457a3d19dba2cf651b7f2a3f") throw $e;
				}
			}
			else {
				$team->addAlliance($alliance);
				if (isset($this->oldmembers[$team->getTeamID()])) {
					unset($this->oldmembers[$team->getTeamID()]);
				}
				else $this->newmembers->append($team);
			}						
		}
		$HT->clearTeams();
		$HT->clearAlliancesMembers();
		$HT->clearAlliancesMembersByLetters();
	}
	
	public function getNewMembers() {
		return $this->newmembers;
	}
	
	public function getMembersLeft() {
		$membersleft = new ArrayObject();
		$dbteam = new DBTeam();
		
		foreach ($this->oldmembers as $team) {
			$membersleft->append($dbteam->getTeamByID($team->getTeamID()));
		}
		
		return $membersleft;
	}
}

?>